﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;


    public class MethodCall
    {

        public static int messageNumber = 0; /*Class Variable across all MethodCalls'*/

        public int paramNumber = 0; 
        public int instanceMessageID { get; set; } /*Instance variable */

        public XmlDocument doc = null;

        public MethodCall(String methodName)
        {

            //Setup of MethodCall Variables
            hasReturned = false; 






            //XML Representation Processing

            doc = new XmlDocument();

            XmlNode documentation = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(documentation);

            /* The Root Element's title is 'methodRequest indicating this is the request portion */
            XmlElement title = doc.CreateElement("methodRequest");


            /* Adding the Child Element 'methodName' to indicate the intended method to be called */
            XmlElement mName = doc.CreateElement("methodName");
            XmlText actualName = doc.CreateTextNode(methodName);
            mName.AppendChild(actualName);
            title.AppendChild(mName);


            /*Adding the number id of the message for future tracking */
            XmlElement mID = doc.CreateElement("messageID");
            XmlText actualId = doc.CreateTextNode(messageNumber.ToString());
            mID.AppendChild(actualId);
            title.AppendChild(mID);


            /*Adding the parameters tag for future use */
            XmlElement mParams = doc.CreateElement("methodParameters");
            title.AppendChild(mParams);



            doc.AppendChild(title);

            /*Incremenet the message Number*/
            instanceMessageID = messageNumber;
            messageNumber++;


        }


        public override String ToString()
        {
            return doc.OuterXml;
            

        }

        /*
        public void addDataParameter(String name, String type, Stream param)
        {

            XmlElement root = doc.DocumentElement;
            XmlElement parameterName = doc.CreateElement(name);
            XmlText parameterData = doc.CreateTextNode(param);

            parameterName.AppendChild(parameterData);

            root.AppendChild(parameterName);
            


        }
        */

        /* addStringParameter will add a named String parameter to the method call. */
        public void AddStringParameterToXml(String param)
        {

            XmlElement root = doc.DocumentElement;
            XmlNode paramRoot = root.LastChild;



            XmlElement parameterName = doc.CreateElement("Parameter" + paramNumber.ToString());
            paramNumber++;


            //Define a type attribute and set it to UTF8 String For this Example
            XmlAttribute parameterType = doc.CreateAttribute("type");
            parameterType.Value = "UTF8String";
            parameterName.Attributes.Append(parameterType); 

            
            //Append The Actual Parameters Content Itself a child of the element
            XmlText parameterData = doc.CreateTextNode(param);
            parameterName.AppendChild(parameterData); 



            //Append to the paramRoot 
            paramRoot.AppendChild(parameterName);


            

        }


        

        public Boolean hasReturned { get; set; }

        public delegate void OnReturnDelegate(String returnValue);

        public OnReturnDelegate OnReturnHandler {get; set;}





    }
